package com.meixiang.adapter.shopping;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.shopping.SpecTagEntity;
import com.meixiang.entity.shopping.SpecTagValueEntity;
import com.meixiang.entity.shopping.result.SpecTagResult;
import com.meixiang.view.flowlayout.CheckedTextViewFlowAdapter;
import com.meixiang.view.flowlayout.CheckedTextViewFlowLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 商品规格标签列表适配器
 *
 * @author LZRUI
 */
public class GoodsSpecAdapter extends RecyclerView.Adapter<GoodsSpecAdapter.TagViewHolder> {

    private List<SpecTagEntity> allTagList; // 全部的规格
    private Map<Integer, Set<Integer>> lastSelectedTagId; // 上次选中的tag
    private TagSelectListener tagSelectListener;

    /**
     * 设置监听
     *
     * @param listener 监听事件
     */
    public void setTagSelectListener(TagSelectListener listener) {
        tagSelectListener = listener;
    }

    /**
     * 刷新数据，根据库存情况改变标签的可选状态
     *
     * @param tagResult       数据
     * @param lastSelectedMap 上次选中的标签
     */
    public void refreshData(SpecTagResult tagResult, Map<Integer, Set<Integer>> lastSelectedMap) {
        if (tagResult != null && tagResult.getSpecList() != null) {
            this.allTagList = tagResult.getSpecList();
        }
        if (lastSelectedMap != null) {
            this.lastSelectedTagId = lastSelectedMap;
        }
        notifyDataSetChanged();
    }

    public GoodsSpecAdapter() {
    }

    @Override
    public TagViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_goods_spec, parent, false);
        return new TagViewHolder(itemView);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onBindViewHolder(TagViewHolder holder, int position) {
        SpecTagEntity entity = allTagList.get(position);
        if (entity == null) return;
        holder.tvTagName.setText(entity.getSpName());
        CheckedTextViewFlowAdapter tagAdapter = new CheckedTextViewFlowAdapter(initAllTag(entity));
        holder.tagFlow.setMaxSelectCount(1);
        tagAdapter.setEnableMap(initEnableMap(entity));
        holder.tagFlow.setAdapter(tagAdapter);
        final int index = holder.getLayoutPosition();
        holder.tagFlow.setOnSelectListener(new CheckedTextViewFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> selectPosSet) {
                if (tagSelectListener != null) {
                    tagSelectListener.onSelected(index, selectPosSet);
                }
            }
        });
        if (this.lastSelectedTagId != null) {
            tagAdapter.setSelectedList(lastSelectedTagId.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return allTagList == null ? 0 : allTagList.size();
    }

    private List<String> initAllTag(SpecTagEntity entity) {
        if (entity == null || entity.getSpecValueList() == null) return null;
        List<String> styles = new ArrayList<>();
        int len = entity.getSpecValueList().size();
        for (int i = 0; i < len; i++) {
            styles.add(entity.getSpecValueList().get(i).getSpValueName());
        }
        return styles;
    }

    /**
     * 设置可以点击的tag
     *
     * @param entity 所有的tag
     * @return 可点击的tag map
     */
    private Map<Integer, Boolean> initEnableMap(SpecTagEntity entity) {
        if (entity == null) return null;
        Map<Integer, Boolean> enableMap = new HashMap<>();
        if (entity.getSpecValueList() != null) {
            int allLen = entity.getSpecValueList().size();
            for (int j = 0; j < allLen; j++) {
                SpecTagValueEntity allValueEntity = entity.getSpecValueList().get(j);
                if (allValueEntity.isExist()) {
                    enableMap.put(j, true);
                }
            }
        }
        return enableMap;
    }

    static class TagViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.tv_tag_name)
        TextView tvTagName;
        @Bind(R.id.tag_flow)
        CheckedTextViewFlowLayout tagFlow;

        public TagViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * 标签选择监听器
     */
    public interface TagSelectListener {
        void onSelected(int specPosition, Set<Integer> selectPosSet);
    }
}
